package name.wilbur.course.db.bigone.service.impl;

import name.wilbur.course.db.bigone.dao.DAOFactory;
import name.wilbur.course.db.bigone.dao.impl.IUserDAO;
import name.wilbur.course.db.bigone.dao.impl.UserDAO;
import name.wilbur.course.db.bigone.model.User;
import name.wilbur.course.db.bigone.service.ILoginService;
import name.wilbur.course.db.bigone.util.IMD5;
import name.wilbur.course.db.bigone.util.MyMD5;

public class LoginService implements ILoginService {
	private IUserDAO userDAO;
	
	public LoginService() {
	}

	@Override
	public boolean isValidUser(User user, String pass) {
		IMD5 md5 = new MyMD5();
		return (user != null && user.getUserPassword().equals(md5.encrypt(pass)));
	}
	
	@Override
	public User findMe(String nameOrEmail) {
		return (User) getUserDAO().findByNameEmail(nameOrEmail);
	}
	
	public IUserDAO getUserDAO() {
		if (userDAO == null)
			userDAO = (UserDAO) DAOFactory.createDAO(UserDAO.class);
		return userDAO;
	}
}
